package cn.clq.ymcc.service.impl;

import cn.clq.ymcc.service.IOrderTokenService;
import cn.clq.ymcc.util.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * @author clqlongbao
 * @version 1.0
 * @description: TODO
 * @date 2022/7/28 16:06
 */
@Service
public class OrderTokenServiceImpl implements IOrderTokenService {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    /**
     * @description: 创建订单的防重token
     * @param: courseId
     * @return: java.lang.String
     * @author clqlongbao
     * @date: 2022/7/28 16:06
     */
    @Override
    public String createOrderToken(String courseId) {
        //获取当前登录人，先写死
        Long loginId = 3L;
        //生成orderToken
        String orderToken = StrUtils.getComplexRandomString(20);
        //将loginId和courseId组合成k，orderToken作为v存到redis
        String k = courseId +":"+loginId;
        String v = orderToken;
        redisTemplate.opsForValue().set(k,v,900,TimeUnit.SECONDS);
        System.out.println(k);
        System.out.println(v);
        return orderToken;
    }
}
